package xyz.xtt.admin.controller;

import jakarta.validation.constraints.NotBlank;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.xtt.admin.constants.CheckLimitTypeEnum;
import xyz.xtt.admin.request.CheckUserDel;
import xyz.xtt.admin.service.ICheckUserLimitService;
import xyz.xtt.admin.vo.CheckUserVo;
import xyz.xtt.common.response.ApiResult;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/admin/config")
@Validated
public class CheckUserLimitController {
    //查询用户对应的封禁记录

    @Autowired
    private ICheckUserLimitService checkUserLimitService;

    @GetMapping("/check_user/get")
    public ApiResult<List<CheckUserVo>> getCheckInfo(String id, String phone) throws Exception {
        Long lId = Optional.ofNullable(id).filter(StringUtils::isNotBlank).map(Long::parseLong).orElse(null);
        return ApiResult.ok(checkUserLimitService.checkLimit(lId, phone));
    }

    @PostMapping("/check_user/del")
    public ApiResult delCheckLimit(@Validated @RequestBody CheckUserDel checkUserDel) {
        Long lId = Long.parseLong(checkUserDel.getId());
        CheckLimitTypeEnum checkLimitTypeEnum = CheckLimitTypeEnum.queryConvertByName(checkUserDel.getType());
        checkUserLimitService.delCheckLimitByLimitType(lId, checkLimitTypeEnum);
        return    ApiResult.ok();
    }


    @GetMapping("/redis/get")
    public  ApiResult<String> getByRedisKey(@Validated @NotBlank(message = "key 不能为空") String key) {
        return  ApiResult.ok(checkUserLimitService.getByRedisKey(key));
    }

    @GetMapping("/redis/del")
    public  ApiResult<Boolean> delByRedisKey(@Validated @NotBlank(message = "key 不能为空") String key) {
        return ApiResult.ok(checkUserLimitService.delByRedisKey(key));
    }
}
